//引入axios
import axios from 'axios'
import Qs from 'qs'
import router from '@/router'
import {Message, MessageBox} from 'element-ui'
import { showLoading, hideLoading } from '../loading';
// import router from '@/router'
var self = this;

/**
 * 统一增加token头文件
 */
axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem('set_token');
        if (token) {
            config.headers.Authorization = token;
            config.headers.common['pms'] = localStorage.getItem("project_id");
        }
        return config;
    },
    error => {
        return Promise.error(error)
    })

//响应拦截器即异常处理
axios.interceptors.response.use(response => response, err => {
    if (err && err.response) {
        switch (err.response.status) {
            case 400:
                err.message = '错误请求'
                break;
            case 401:
                err.message = '账户已被登录或已过期'
                break;
            case 403:
                err.message = '拒绝访问'
                break;
            case 404:
                err.message = '请求错误,未找到该资源'
                break;
            case 405:
                err.message = '请求方法未允许'
                break;
            case 408:
                err.message = '请求超时'
                break;
            case 500:
                err.message = '服务器端出错'
                break;
            case 501:
                err.message = '网络未实现'
                break;
            case 502:
                err.message = '网络错误'
                break;
            case 503:
                err.message = '服务不可用'
                break;
            case 504:
                err.message = '网络超时'
                break;
            case 505:
                err.message = 'http版本不支持该请求'
                break;
            default:
                err.message = `连接错误${err.response.status}`
        }
    } else {
        err.message = "网络波动..."
    }
    Message.error(err.message);
    if (err.response.status === 401) {
        router.push('/');
    }
    return Promise.resolve(err.response)
})

// 后台接口地址
axios.defaults.baseURL = sessionStorage.getItem("base_url");
axios.defaults.timeout = 1000000;

export function get(url, param) {
    return new Promise((resolve, reject) => {
        axios({
            method: 'get',
            url,
            params: param
        }).then(res => {
            resolve(res)
        })
    });
}

//post请求
export function post(url, p) {
	showLoading()//显示加载中
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url,
            headers: {
                'Content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
            data: Qs.stringify(p)
        }).then(res => {
			hideLoading()//关闭加载
            resolve(res)
        })
    })
}

//post请求
export function save(url, p) {
    showLoading()//显示加载中
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url,
            headers: {
                'Content-type': 'application/json',
            },
            data: p
        }).then(res => {
            hideLoading()//关闭加载
            resolve(res)
        })
    })
}

//post请求
export function deletep(url, p) {
    showLoading()//显示加载中
    return new Promise((resolve, reject) => {
        axios({
            method: 'delete',
            url,
            headers: {
                'Content-type': 'application/x-www-form-urlencoded',
            },
            data: Qs.stringify(p)
        }).then(res => {
            hideLoading()//关闭加载
            resolve(res)
        })
    })
}

/*PUT请求*/
export function put(url, p) {
    showLoading()//显示加载中
    return new Promise((resolve, reject) => {
        axios({
            method: 'put',
            url,
            headers: {
                'Content-type': 'application/json',
            },
            data: p
        }).then(res => {
            hideLoading()//关闭加载
            resolve(res)
        })
    })
}

/**
 * put操作非JSON格式
 *
 * @param url
 * @param p
 * @returns {Promise<unknown>}
 */
export function putX(url, p) {

    return new Promise((resolve, reject) => {
        axios({
            method: 'put',
            url,
            headers: {
                'Content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
            data: Qs.stringify(p)
        }).then(res => {
            resolve(res)
        })
    })
}

/*post上传请求*/
export function upload(url, p) {
    showLoading()//显示加载中
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url,
            headers: {
                'Content-Type': 'multipart/form-data',
            },
            data: p
        }).then(res => {
            hideLoading()//关闭加载
            resolve(res)
        })
    })
}


